/*
* $Id: SettingsItem.java 270 2011-02-20 13:59:40Z volok-aleksej@yandex.ru $
*
* Copyright 2008-2011 by Volokitin Alexey
* All rights reserved
*
*/
package apiary.component.settings;

import apiary.settings.SettingStore;
import apiary.settings.SettingsRecord;
import apiary.settings.SettingsRecord.Field;
import apiary.ui.CategoryItem;
import apiary.ui.SettingForm;
import apiary.common.FormHelper;
import java.util.Vector;

/**
 * Common interface that describe setting
 * @author lexus
 */
public abstract class SettingsItem {

    public static final String STATE_SET = "set";
    public static final String STATE_UNSET = "unset";

    public static final String ID_PARENT = "parent";

    /** Settings form */
    protected SettingForm settingsForm;
    /** Setting. It is visual part */
    protected CategoryItem mainCategory;
    /** List of setting items. It is visual part */
    protected Vector settingCategories;

    /**
     * Constructor
     * @param form form of settings
     */
    public SettingsItem(SettingForm form) {
        settingsForm = form;
        mainCategory = null;
        settingCategories = new Vector();
    }

    /**
     * Gets image path of setting item
     * @param id identificator of item
     * @param set state of item
     * @return image path
     */
    public static String GetImageItem(String id, boolean set){
        if(ID_PARENT.equals(id)){
            if(set)
                return "/res/ArrowDown.png";
            else
                return "/res/ArrowRight.png";
        } else {
            if (set)
                return "/res/CheckboxCheck.png";
            else
                return "/res/Checkbox.png";
        }
    }

    /**
     * Gets setting.
     * @return setting(visual part)
     */
    public CategoryItem getMainCategory(){
        return mainCategory;
    }

    /**
     * Gets list of setting items
     * @return list of setting items
     */
    public Vector getSettingCategories(){
        return settingCategories;
    }

    /**
     * Saving value of item setting
     * @param name identificator of item
     * @param value value of item
     */
    protected void saveItem(Integer name, Object value){
        SettingStore store = SettingStore.getInstance();
        SettingsRecord.Field field = (Field) store.getData(name);
        field.setValue(value);
    }

    /**
     * Gets value of item setting
     * @param name identificator of item
     * @return value of item
     */
    protected Object getItem(Integer name){
        SettingStore store = SettingStore.getInstance();
        SettingsRecord.Field field = (Field) store.getData(name);
        return field.getValue();
    }
    /**
     * Gets item setting(visual part)
     * @param id identificator item
     * @param name visual name item
     * @param set state item
     * @return item setting(visual part)
     */
    protected CategoryItem getCategoryItem(String id, String name, boolean set){
        CategoryItem item = FormHelper.createCategoryItem(settingsForm,
                id, name, GetImageItem(id, set));
        item.setState(set ? STATE_SET : STATE_UNSET);
        return item;
    }
}
